package com.hsylient.demo03;

import lombok.SneakyThrows;
import org.junit.Test;

import java.sql.*;
import java.util.Date;

public class TestPreparedStatement {
    private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=true";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";

    @Test
    @SneakyThrows
    public void testInsert() {
        Class.forName(DRIVER);
        Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        String sql = "insert into user(name, age, email, birthday) values (?, ?, ?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "hsylient");
        preparedStatement.setInt(2, 22);
        preparedStatement.setString(3, "1617515402@qq.com");
        preparedStatement.setObject(4, new Date());
        int affectCount = preparedStatement.executeUpdate();
        System.out.println(affectCount);
    }

    @Test
    public void testDelete() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            String sql = "delete from user where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, 9);
            int affectCount = preparedStatement.executeUpdate();
            System.out.println(affectCount == 1 ? "删除成功" : "删除失败");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    @Test
    @SneakyThrows
    public void testUpdate() {
        Class.forName(DRIVER);
        Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        String sql = "update user set name = '哮天犬' where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1, 10);
        int affectCount = preparedStatement.executeUpdate();
        System.out.println(affectCount == 1 ? "更新成功" : "更新失败");
        preparedStatement.close();
        connection.close();
    }

    @Test
    @SneakyThrows
    public void testQuery() {
        Class.forName(DRIVER);
        Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        String sql = "select * from user where `name` = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "哮天犬");
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String id = resultSet.getString("id");
            String username = resultSet.getString("username");
            String password = resultSet.getString("password");
            System.out.printf("id: %s username: %s, password: %s%n", id, username, password);
        }
        preparedStatement.close();
        connection.close();
    }
}
